package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

//厂家的服务实现类
@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private FactoryDao factoryDao;


    //厂家的分页列表
    @Override
    public PageInfo<Factory> findByPage(FactoryExample factoryExample, int pageNum, int pageSize) {
        //1. 设置开始页与页面大小
        PageHelper.startPage(pageNum, pageSize);
        //2. 设置所有
        List<Factory> list = factoryDao.selectByExample(factoryExample);
        //3 /.构建一个PageInfo
        PageInfo<Factory> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //根据条件查询全部的厂家
    @Override
    public List<Factory> findAll(FactoryExample factoryExample) {
        return factoryDao.selectByExample(factoryExample);
    }

    //根据id查找厂家
    @Override
    public Factory findById(String id) {
        Factory factory = factoryDao.selectByPrimaryKey(id);
        return factory;
    }

    //添加厂家
    @Override
    public void save(Factory factory) {
        //设置主键
        factory.setId(UUID.randomUUID().toString());
        factory.setCreateTime(new Date());
        factory.setUpdateTime(new Date());
        factoryDao.insertSelective(factory);
    }

    //更新厂家
    @Override
    public void update(Factory factory) {
        factory.setUpdateTime(new Date());
        factoryDao.updateByPrimaryKeySelective(factory);
    }

    //根据主键删除厂家
    @Override
    public void delete(String id) {
        factoryDao.deleteByPrimaryKey(id);
    }


    //根据厂家的名字查询厂家
    @Override
    public Factory findByFactoryName(String factoryName) {
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andFactoryNameEqualTo(factoryName);
        List<Factory> factoryList = factoryDao.selectByExample(factoryExample);
        if (factoryList.size() > 0) {
            return factoryList.get(0);
        }
        return null;
    }

}
